/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.card.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.card.dto.RiskCardNotifyEntityDTO;
import com.hopes.bizz.risk.card.vo.RiskCardNotifyEntityVO;
import com.hopes.bizz.risk.control.entity.RiskObjectEntity;
import com.hopes.bizz.risk.control.vo.RiskObjectVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.risk.card.entity.RiskCardNotifyEntity;
import com.hopes.bizz.risk.card.service.RiskCardNotifyService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 有害因素应知卡
 *
 * @author cx
 * @date 2023-08-03 16:01:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskCardNotify" )
@Tag(description = "riskCardNotify" , name = "有害因素应知卡管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskCardNotifyController {

    private final  RiskCardNotifyService riskCardNotifyService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param riskCardNotify 有害因素应知卡
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('card_risk_card_notify_view')" )
    public R getriskCardNotifyPage(Page page, RiskCardNotifyEntity riskCardNotify) {
        LambdaQueryWrapper<RiskCardNotifyEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(riskCardNotify.getCardNo()),RiskCardNotifyEntity::getCardNo,riskCardNotify.getCardNo());
		wrapper.eq(StrUtil.isNotBlank(riskCardNotify.getCardName()),RiskCardNotifyEntity::getCardName,riskCardNotify.getCardName());
        return R.ok(riskCardNotifyService.page(page, wrapper));
    }


    /**
     * 通过id查询有害因素应知卡
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('card_risk_card_notify_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        RiskCardNotifyEntity entity = riskCardNotifyService.getById(id);
        RiskCardNotifyEntityVO vo = new RiskCardNotifyEntityVO();
        BeanUtils.copyProperties(entity, vo);
        return R.ok(vo);
    }

    /**
     * 新增有害因素应知卡
     * @param riskCardNotifyEntityDTO 有害因素应知卡
     * @return R
     */
    @Operation(summary = "新增有害因素应知卡" , description = "新增有害因素应知卡" )
    @SysLog("新增有害因素应知卡" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('card_risk_card_notify_add')" )
    public R save(@RequestBody RiskCardNotifyEntityDTO riskCardNotifyEntityDTO) {
        return R.ok(riskCardNotifyService.save(riskCardNotifyEntityDTO.toPO()));
    }

    /**
     * 修改有害因素应知卡
     * @param riskCardNotifyEntityDTO 有害因素应知卡
     * @return R
     */
    @Operation(summary = "修改有害因素应知卡" , description = "修改有害因素应知卡" )
    @SysLog("修改有害因素应知卡" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('card_risk_card_notify_edit')" )
    public R updateById(@RequestBody RiskCardNotifyEntityDTO riskCardNotifyEntityDTO) {
        return R.ok(riskCardNotifyService.updateById(riskCardNotifyEntityDTO.toPO()));
    }

    /**
     * 通过id删除有害因素应知卡
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除有害因素应知卡" , description = "通过id删除有害因素应知卡" )
    @SysLog("通过id删除有害因素应知卡" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('card_risk_card_notify_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskCardNotifyService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param riskCardNotify 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('card_risk_card_notify_export')" )
    public List<RiskCardNotifyEntity> export(RiskCardNotifyEntity riskCardNotify) {
        return riskCardNotifyService.list(Wrappers.query(riskCardNotify));
    }
}